package com.onlineproject.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
//import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//import org.springframework.web.context.support.WebApplicationContextUtils;

public class HibernateUtils {

	private static SessionFactory factory;
	
	static {
		try {
			//Configuration cfg = new Configuration().configure();
			//factory = cfg.buildSessionFactory();
			//BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
			factory = (SessionFactory)new ClassPathXmlApplicationContext("applicationContext.xml").getBean("sessionFactory");
		
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static SessionFactory getSessionFactory() {
		return factory;
	}
	
	public static Session getSession() {
		return factory.openSession();
	}
	
	public static void closeSession(Session session) {
		if (session != null) {
			if (session.isOpen()) {
				session.close();
			}
		}
	}
}
